﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Windows.Browser;

namespace SlvPartecipazione
{
    public static class Utils
    {
        public static string getGUID()
        {
            if (HtmlPage.Document.QueryString.ContainsKey("guid"))
            {
                string queryStringValue = HtmlPage.Document.QueryString["guid"];
                if (queryStringValue.IsGUIDValid())
                {
                    return queryStringValue;
                }
            }
#if DEBUG
            return string.Empty;
#else
            return string.Empty;
#endif
        }

        /// <summary>
        /// Converts a coordinate from the polar coordinate system to the cartesian coordinate system.
        /// </summary>
        /// <param name="angle"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        public static Point ComputeCartesianCoordinate(double angle, double radius)
        {
            // convert to radians
            double angleRad = (Math.PI / 180.0) * (angle - 90);

            double x = radius * Math.Cos(angleRad);
            double y = radius * Math.Sin(angleRad);

            return new Point(x, y);
        }

        public static Point Offset(this Point point, double X, double Y)
        {
            return new Point(point.X + X, point.Y + Y);
        }

        //////////////////////////////////////

        public static bool IsGUIDValid(this string expression)
        {
            if (expression != null)
            {
                Regex guidRegEx = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");

                return guidRegEx.IsMatch(expression);
            }
            return false;
        }

        public static bool IsTimeStampValid(this string expression)
        {
            if (expression != null)
            {
                Regex guidRegEx = new Regex(@"[0-9]*/[0-9]*/[0-9]* [0-9]*:[0-9]*");

                return guidRegEx.IsMatch(expression);
            }
            return false;
        }
    }
}
